package com.kgc.scd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.service.WebShopUserService;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.vo.ShopUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created On : 2022/11/16.
 * <p>
 * Author : 刘慧枫
 * <p>
 * Description: web端登录入口
 */
@Slf4j
@RestController
@Api(tags = "web端登录入口，版本1.0")
public class WebLoginController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private WebShopUserService webShopUserService;
    /**
     * @author : 刘慧枫
     * @date   : 2022/11/15
     * @param  : [userName, userPwd]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 手机号快捷注册
     */
    @PostMapping("/doRegisterWeb")
    @ApiOperation(value = "手机号快捷注册",notes = "基于mybatis-plus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "账号"),
            @ApiImplicitParam(name = "userPwd",value = "密码"),
            @ApiImplicitParam(name = "userPhone",value = "手机号"),
            @ApiImplicitParam(name = "verifyCode",value = "验证码"),
            @ApiImplicitParam(name = "userNickName",value = "昵称")
    })
    public RequestResult<String> doRegister(@RequestParam String userName,
                                            @RequestParam String userPwd,
                                            @RequestParam String userPhone,
                                            @RequestParam String verifyCode,
                                            @RequestParam String userNickName){
        log.info("------ 手机号快捷注册 ------");
        return webShopUserService.doRegisterUseFeign(userName,userPwd,userPhone,verifyCode,userNickName);
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/14
     * @param  : [userName, userPwd]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 账号密码登录
     */
    @PostMapping("/loginWeb")
    @ApiOperation(value = "账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "账号"),
            @ApiImplicitParam(name = "userPwd",value = "密码")
    })
    public RequestResult<String> doLogin(@RequestParam String userName,
                                         @RequestParam String userPwd){
        log.info("------ 账号密码登录 ------");
        return webShopUserService.doLoginUseFeign(userName,userPwd);
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/14
     * @param  : [userName, userPwd]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 手机号快捷登录
     */
    @PostMapping("/doLoginByPhoneWeb")
    @ApiOperation(value = "手机号快捷登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone",value = "手机号"),
            @ApiImplicitParam(name = "verifyCode",value = "验证码")
    })
    public RequestResult<String> doLoginByPhone(@RequestParam String userPhone,
                                                @RequestParam String verifyCode){
        log.info("------ 手机号快捷登录 ------");
        return webShopUserService.doLoginByPhoneUseFeign(userPhone,verifyCode);
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/10/28
     * @param  : []
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 微信扫码登录
     */
    @GetMapping("/wechat/auth/codeUrlWeb")
    @ApiOperation(value = "微信扫码登录")
    public RequestResult<String> codeUrl(){
        log.info("------ 微信扫码登录 ------");
        return webShopUserService.codeUrlUseFeign();
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/16
     * @param  : [openId, userPhone]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 微信绑定手机号
     */
    @PostMapping("/bindUserPhoneWeb")
    @ApiOperation(value = "微信绑定手机号",notes = "支持token自动鉴权")
    @TokenPermission
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone",value = "手机号"),
            @ApiImplicitParam(name = "verifyCode",value = "验证码")
    })
    public RequestResult<String> bindUserPhone(@RequestParam String userPhone,
                                               @RequestParam String verifyCode,
                                               @ApiIgnore @RequestShopUser ShopUser shopUser){
        log.info("------ 微信绑定手机号 ------");
        return webShopUserService.bindUserPhoneUseFeign(userPhone,verifyCode,shopUser);
    }

    /**
     * @author : 刘慧枫
     * @date   : 2022/11/14
     * @param  : [phone]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 短信验证码
     */
    @GetMapping("/phoneWeb")
    @ApiOperation(value = "短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号")
    })
    public RequestResult<String> sendMessage(@RequestParam String phone) {
        log.info("------ 短信验证码 ------");
        return webShopUserService.sendMessageUseFeign(phone);
    }

}
